/**
* The ServerThreadHandler Class reads information from the client and sends game information back to the client.
* Group: Y1
* @author Mason Kwiat
*/


//package network;
package lightspeed.Network;
import java.net.*;
import java.io.*;

import lightspeed.Backend.GameManager;

public class ServerThreadHandler
{

	private Socket socket = null; 

	private int num; 

	private boolean connected;
	
	/**
	* Constructor: creates a ServerThreadHandler instance.
	* Creates a Connection to a given client (socket s). Creates a connection for the n'th client that tries to connect to the server.
	*/
	public ServerThreadHandler(Socket s, int n)
	{
		num = n;
		socket = s;
		connected = true;
		
		// create threads for input and output and start them
		ReadInput reader = this.new ReadInput();
	    SendOutput sender = this.new SendOutput();
	    sender.start();
	    reader.start();
	    //
	}
	
	
	//private classes//
	
	
	private class ReadInput extends Thread
	{
		public void run()
		{
			ObjectInputStream objIn = null;
			try
			{
				//get object stream from socket
				objIn = new ObjectInputStream(socket.getInputStream());
				//
				
				while(connected) //read loop
	    		{
	    			//read in next packet
					ClientPacket cp;
					try
					{
						cp = (ClientPacket) objIn.readObject(); //listen for next object from stream
						LightServer.cpackets[num] = cp;
						
//						System.out.println("message is: " + cp.getMessage());
						
						if(cp.message != "")
							LightServer.spacket.messages[num] = cp.name + ": " + cp.message;
						
//						System.out.println("read packet from " + num + "--message: " + LightServer.spacket.messages[num]);
					}
					catch(Exception e)
					{
						e.printStackTrace();
						connected = false;
					}
					//
					
					//wait specified number of milliseconds
//	    			try
//	    			{
//	    				sleep(200);
//	    			}
//	    			catch(Exception e)
//	    			{
//	    				e.printStackTrace();
//	    			}
	    			//
	    		}
	    		
	    		//cleanup
	    		objIn.close();
	    		socket.close();
	    		//
			}
			catch(IOException e)
	    	{
	    		e.printStackTrace();
	    	}
		}
	}
	
	private class SendOutput extends Thread
	{
		int threadWait = GameManager.GAME_SPEED;
		
		public void run()
		{
			ObjectOutputStream objOut = null;
			try
			{
				//create object output stream for client socket
				objOut = new ObjectOutputStream(socket.getOutputStream());
				//
				
				//send client their connection number
				objOut.writeObject(new Integer(num));
				//
				
				while(connected)
				{
					int t = (int)System.currentTimeMillis(); //get current time
					
					//get ServerPacket object and write it to stream
					ServerPacket sp = LightServer.spacket;
					
					objOut.reset();
					objOut.writeObject(sp);
					//
					
					//System.out.println("server send lag: " + ((int)System.currentTimeMillis() - t));
					if((int)System.currentTimeMillis() - t > threadWait) //check if packet sending took longer than threaWait
					{
						threadWait = 5; //minimum wait
					}
					else
					{
						threadWait = threadWait - ((int)System.currentTimeMillis() - t); //subtract time difference from threadWait (wait time until next packet send)
					}
					
					//wait specified number of milliseconds
	    			try
	    			{
	    				sleep(threadWait);
	    			}
	    			catch(Exception e)
	    			{
	    				e.printStackTrace();
	    			}
	    			//
	    			
	    			threadWait = GameManager.GAME_SPEED; //reset threadWait variable
				}
				
				//cleanup
				objOut.close();
				socket.close();
				//
			}
			catch(IOException e)
	    	{
	    		e.printStackTrace();
	    	}
		}
	}
}
